//
//  MMGHomePageCell.swift
//  MyMicroblog
//
//  Created by Mac on 17/1/7.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import YYImage
import SDWebImage

// 设置图片宽度和图片间距
let margin:CGFloat = 8
let marginImage:CGFloat = 2
let imageWidth:CGFloat = (ScreenWidth - 2 * marginImage - 2 * margin) / 3

class MMGHomePageCell: UITableViewCell {
    // 用户头像
    @IBOutlet weak var avatar_largeView: UIImageView!
    // 用户名称
    @IBOutlet weak var nameLabel: UILabel!
    // 用户认证类型
    @IBOutlet weak var verified_typeView: UIImageView!
    // 用户等级
    @IBOutlet weak var mbrankView: UIImageView!
    // 微博发布时间
    @IBOutlet weak var created_atLabel: UILabel!
    // 微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    // 微博内容
    @IBOutlet weak var contentLabel: UILabel!
    // 图片视图
    @IBOutlet weak var pic_urlsView: MMGPictureView!
    // 视图框高度
    @IBOutlet weak var pictureViewHeight: NSLayoutConstraint!
    // 视图框宽度
    @IBOutlet weak var pictureViewWidth: NSLayoutConstraint!
    // 视图框顶部距离
    @IBOutlet weak var pictureViewTop: NSLayoutConstraint!
    // 布局对象
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
    // 底部工具栏
    @IBOutlet weak var bottomToolView: UIView!
    // 转发文字
    @IBOutlet weak var retweetedTextLabel: UILabel!
    
    // 获取 viewModel, 处理视图信息
    var viewModel: MMGStatusViewModel? {
    
        didSet{
        // 获取 viewModel 信息,为控件赋值
            avatar_largeView.sd_setImage(with: viewModel?.urlAvater)
            verified_typeView.image = viewModel?.verified_typeImage
            mbrankView.image = viewModel?.mbrankImage
            nameLabel.text = viewModel?.status?.user?.name
//            created_atLabel.text = viewModel?.status?.created_at
//            sourceLabel.text = viewModel?.status?.source
            created_atLabel.text = viewModel?.timeText
            sourceLabel.text = viewModel?.sourceText
            contentLabel.text = viewModel?.status?.text
            // 获取图片视图中图片的个数
            let pictureCount: Int = (viewModel?.imageCount)!
            // 设置视图约束
            pictureViewTop.constant = (pictureCount == 0 ? 0 : margin)
            // 根据图片的个数计算视图尺寸
            let viewSize:CGSize = creatPic_urlsViewSize(count: pictureCount)
            pictureViewWidth.constant = viewSize.width
            pictureViewHeight.constant = viewSize.height
            // 为视图空间赋值
            pic_urlsView.pictureInfos = viewModel?.imageURLs
            // 为转发微博消息赋值
            if viewModel?.status?.retweeted_status != nil {
                retweetedTextLabel.text = viewModel?.status?.retweeted_status?.text ?? ""
                retweetedTextLabel.numberOfLines = 0
                retweetedTextLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
            }
        }
    }
    
    // 设置图片视图的尺寸和约束
    private func creatPic_urlsViewSize(count: Int) -> CGSize {
        collectionViewFlowLayout.itemSize = CGSize(width: imageWidth, height: imageWidth)
        
        if count == 0 {
            return CGSize.zero
        }
        
        if count == 1{
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.imageURLs?.last?.wap360Str ?? "")
            let imageSize = image?.size
            collectionViewFlowLayout.itemSize = imageSize ?? CGSize.zero
            return imageSize ?? CGSize.zero
        }
        
        let colN: Int = count < 3 ? count : 3
        let rowN: Int = count / 4 + 1
        
        let viewWidth = imageWidth * CGFloat(colN) + marginImage * CGFloat(colN - 1)
        let viewHeight = imageWidth * CGFloat(rowN) + marginImage * CGFloat(rowN - 1)
        return CGSize(width: viewWidth, height: viewHeight)
    }
    
    // 构造方法
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 设置微博消息属性
        contentLabel.numberOfLines = 0
        contentLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        
        // 设置单元格属性
        collectionViewFlowLayout.itemSize = CGSize(width: imageWidth, height: imageWidth)
        collectionViewFlowLayout.minimumLineSpacing = marginImage
        collectionViewFlowLayout.minimumInteritemSpacing = marginImage
        
        // 设置单元格点击事件
        selectionStyle = .none
    }

}
